package algorithms.que13;

import java.util.Iterator;
import java.util.ListIterator;

/**
 * @功能
 * @创建时间 2020/7/11
 */
public class DoubleNode<Item> {
    Node first;
    Node last;
    public void addFirst(Item item){
        if(first.item==null){
            first.item=item;
            last.item=item;
        }else if(first.next==null&&last.pre==null){
            first.next=last;
            first.item=item;
            last.pre=first;
        }else {
            Node oldfirst = new Node();
            oldfirst.item=first.item;
            oldfirst.next=first.next;
            oldfirst.pre=first;
            if (last.pre==first){
                last.pre=oldfirst;
            }
            first.item=item;
            first.next=oldfirst;
        }

    }
    public void delFirst(){
        if(first.next!=null&&first.next!=last){
            first.item=first.next.item;
            first.next=first.next.next;
            first.next.next.pre=first;
        }else if(first.next==last){
            first.item=last.item;
            first.next=null;
            last.pre=null;

        }
    }
    public void addLast(Item item){
        if(last.item==null){
            first.item=item;
            last.item=item;
        }else if(first.next==null&&last.pre==null){
            first.next=last;
            last.item=item;
            last.pre=first;
        }else {
            Node oldLast = new Node();
            oldLast.item=last.item;
            oldLast.pre=last.pre;
            oldLast.next=last;
            if(first.next==last){
                first.next=oldLast;
            }
            last.item=item;
            last.pre=oldLast;
        }
    }
    public void addMiddle(Item item,Node a){
        Node cur = new Node();
        cur.item=item;
        cur.next=a;
        cur.pre=a.pre;
        a.pre.next=cur;
        a.pre=cur;
    }
    public void delMiddle(Node a){
        a.pre.next=a.next;
        a.next.pre=a.pre;
    }
    public class Node{
        Node next;
        Item item;
        Node pre;

    }
}
